﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.IDAL;
using OgilvyOne.Model;
using OgilvyOne.DALFactory;

namespace OgilvyOne.BLL
{
    public class MemberInfoBll
    {

        public const string MEMBER_LOGIN_INFO = "MEMBER_LOGIN_INFO";
        /// <summary>
        /// 会员注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Reg(MemberInfoModel model)
        {
            int mid= MemberInfoFactory.create().Add(model);
            if (mid > 0)
            {
                model.Memberid = mid;
                System.Web.HttpContext.Current.Session.Add(MEMBER_LOGIN_INFO, model); //登录
                return true;
            }
            return false;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string username, string password)
        {
            return Login(username, password, "1");//官网登录
        }
        public bool Login(string username, string password, string usertype)
        {
            MemberInfoModel mm = GetMemberInfoModel(username, usertype);
            if (mm != null)
            {
                if (mm.Password == password)
                {
                    System.Web.HttpContext.Current.Session.Add(MEMBER_LOGIN_INFO, mm);
                    MemberInfoFactory.create().UpdateLastLoginTime(mm.Memberid, DateTime.Now);
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 修改基本资料
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(MemberInfoModel model)
        {
            return MemberInfoFactory.create().Update(model);
        }
       
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="newpas"></param>
        /// <returns></returns>
        public bool UpdatePassword(int mid, string newpas)
        {
            return MemberInfoFactory.create().UpdatePassword(mid, newpas);
        }
        /// <summary>
        /// 获得用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userType">合作网站类型</param>
        /// <returns></returns>
        public MemberInfoModel GetMemberInfoModel(string username, string userType)
        {
            return MemberInfoFactory.create().GetMemberInfoModel(username, userType);
        }
        /// <summary>
        /// 获得用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userType"></param>
        /// <returns></returns>
        public MemberInfoModel GetMemberInfoModel(string username)
        {
            return MemberInfoFactory.create().GetMemberInfoModel(username);
        }
        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userType">合作网站类型</param>
        /// <returns></returns>
        public bool IsExists(string username, string userType)
        {
            return MemberInfoFactory.create().IsExists(username, userType);
        }
        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool IsExists(string username)
        {
            return MemberInfoFactory.create().IsExists(username);
        }
        /// <summary>
        /// 查询会员
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public IList GetUserList(MemberInfoModel q)
        {
            return MemberInfoFactory.create().GetUserList(q);
        }

        public MemberInfoModel GetMemberInfoModel(int mid)
        {
            return MemberInfoFactory.create().GetMemberInfoModel(mid);
        }

        public bool UpdateState(int mid, int state)
        {
            return MemberInfoFactory.create().UpdateState(mid, state);
        }
    }
}
